//
//  COCallGiftTableCell.swift
//  CocoProject
//
//  Created by Vill on 2023/11/15.
//

import UIKit

class COCallGiftTableCell: UITableViewCell {

    var gift: COGiftContent? {
        didSet {
            setupContent()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        initSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initSubviews() {
        contentView.addSubview(container)
        container.addSubview(background)
        container.addSubview(iconView)
        container.addSubview(nickLabel)
        container.addSubview(streamerNameLabel)
        container.addSubview(giftIconView)
    }

    private func setupContent() {
        guard gift != nil else { return }
        let userInfo = gift?.senderUserInfo
        if let avatar = userInfo?.portraitUri {
            iconView.sd_setUrlImage(avatar, placeholder: KImaged("coco_icon_holder"))
        }
        nickLabel.text = userInfo?.name
        giftIconView.sd_setUrlImage(gift?.cover)
        streamerNameLabel.text = "send a gift to \(gift?.toStreamerName ?? "")"
    }
    
    lazy var container: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 285, height: 85))
        return view
    }()
    
    lazy var background: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 25, width: 195, height: 60))
        view.backgroundColor = .init(white: 0, alpha: 0.4)
        view.clipsAllCorners(radius: 5)
        return view
    }()
    
    lazy var iconView: UIImageView = {
        let view = UIImageView(frame: CGRect(x: 10, y: background.mj_y+6, width: 26, height: 26))
        view.contentMode = .scaleAspectFill
        view.clipsAllCorners(radius: 13)
        return view
    }()
    
    lazy var nickLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: iconView.right+5, y: iconView.mj_y + 3, width: 80, height: 20))
        label.font = UIFont.pingfangFont(ofSize: 15, weight: .bold)
        label.textColor = .white
        return label
    }()
    
    lazy var streamerNameLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 10, y: background.mj_y+33, width: 265, height: 30))
        label.font = UIFont.pingfangFont(ofSize: 15, weight: .medium)
        label.textColor = .white
        return label
    }()
    
    lazy var giftIconView: UIImageView = {
        let view = UIImageView(frame: CGRect(x: 127, y: 0, width: 68, height: 68))
        view.contentMode = .scaleAspectFit
        return view
    }()
}
